package com.tip.calculator.view;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.tip.calculator.R;
import com.tip.calculator.model.Tip;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class TipTailoring extends Activity {

	private static final String NUM_OF_GUESTS = "numberOfGuests";
	private static final String TOTAL_TIP = "totalTip";
	private static final String PER_PERSON_TIP = "perPersonTip";
	private static final String BILL_TOTAL = "billTotal";
	private static final String MY_TIP = "myTip";

	private Tip tip;
	private int numberOfGuests;
	private float totalTip;
	private float perPersonTip;
	private float billTotal;
	private ArrayList<EditText> editTextList;
	private ArrayList<SeekBar> seekBarList;
	private ArrayList<TextView> textViewList;
	private int[] guestProgress;
	private DecimalFormat dec;
	private CheckBox tipSplit, billSplit;
	private ScrollView sv;
	private LinearLayout lv, ll, splitLayout;
	private TextView tv;
	private Bundle bundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		dec = new DecimalFormat("##.##");

		bundle = this.getIntent().getExtras();
		tip = (Tip) bundle.getSerializable(MY_TIP);
		numberOfGuests = tip.getNumberOfGuests();
		totalTip = Float.parseFloat(dec.format(tip.getTotalTip()));
		perPersonTip = Float.parseFloat(dec.format(tip.getPerPersonTip()));
		billTotal = Float.parseFloat(dec.format(tip.getBillTotal()));

		editTextList = new ArrayList<EditText>();
		seekBarList = new ArrayList<SeekBar>();
		textViewList = new ArrayList<TextView>();
		guestProgress = new int[numberOfGuests];

		sv = new ScrollView(this);
		ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		sv.addView(ll);

		tv = new TextView(this);
		tv.setText("Split:");
		tv.setTextSize(40);
		ll.addView(tv);

		/*
		 * ******************Linear Layout for Split*********************
		 */
		splitLayout = new LinearLayout(this);
		splitLayout.setOrientation(LinearLayout.HORIZONTAL);

		tipSplit = new CheckBox(this);
		tipSplit.setText("Tip");
		tipSplit.setPadding(50, 0, 0, 0);
		splitLayout.addView(tipSplit);

		billSplit = new CheckBox(this);
		billSplit.setText("Bill");
		billSplit.setPadding(50, 0, 0, 0);
		splitLayout.addView(billSplit);

		ll.addView(splitLayout);

		tipSplit.setChecked(true);
		/*
		 * ******************Linear Layout for Split*********************
		 */
		EditText et = new EditText(this);
		et.setText("" + totalTip);
		ll.addView(et);

		for (int i = 0; i < numberOfGuests; i++) {
			LinearLayout l = new LinearLayout(this);
			l.setOrientation(LinearLayout.HORIZONTAL);

			EditText name = new EditText(this);
			name.setWidth(180);
			name.setHeight(LayoutParams.FILL_PARENT);
			name.setId(i);
			name.setHint("Enter name");
			l.addView(name);
			editTextList.add(name);

			SeekBar tipSeekBar = new SeekBar(this);

			int percentFromTip = getPercentFromTip(perPersonTip);
			tipSeekBar.setProgress(percentFromTip);
			guestProgress[i] = percentFromTip;

			tipSeekBar.setId(i);
			tipSeekBar.setMax(100);
			tipSeekBar.setPadding(10, 10, 0, 0);
			LayoutParams lp = new LayoutParams(175, 55);
			tipSeekBar.setLayoutParams(lp);
			l.addView(tipSeekBar);
			seekBarList.add(tipSeekBar);

			TextView amount = new TextView(this);
			amount.setText("$\t" + perPersonTip);
			amount.setWidth(100);
			amount.setId(i);
			amount.setPadding(10, 0, 0, 0);
			l.addView(amount);
			textViewList.add(amount);

			ll.addView(l);
		}

		this.setContentView(sv);

		for (int i = 0; i < numberOfGuests; i++) {

			final TextView t = textViewList.get(i);
			final int j = i;
			SeekBar s = seekBarList.get(i);

			s.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

					if ((fromUser) && (numberOfGuests > 1)) {
						int difference = 100 - progress;
						int newProgressPerPerson = difference / (numberOfGuests - 1);

						textViewList.get(j).setText("" + progress);
						guestProgress[j] = progress;

						for (int k = 0; k < numberOfGuests; k++) {
							if (k != j) {
								int userProgress = guestProgress[k];
								userProgress = newProgressPerPerson;

								// print it out
								float newTip = getTipFromPercent(userProgress);
								textViewList.get(k).setText("" + userProgress);
								guestProgress[k] = userProgress;

								seekBarList.get(k).setProgress(userProgress);

							}
						}

						updateAllSplits();
					}
				}

				public void onStartTrackingTouch(SeekBar arg0) {
					// TODO Auto-generated method stub

				}

				public void onStopTrackingTouch(SeekBar arg0) {
					// TODO Auto-generated method stub
				}
			});
		}

	}

	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	public int getPercentFromTip(Float tip) {
		int percent;
		percent = (int) ((tip / totalTip) * 100);
		return percent;
	}

	public float getTipFromPercent(int percent) {
		float tip = ((float) percent / 100) * totalTip;
		return tip;
	}

	public float getBillFromPercent(int percent) {
		float tip = ((float) percent / 100) * billTotal;
		return tip;
	}

	public float getBillAndTipFromPercent(int percent) {
		float tip = ((float) percent / 100) * (totalTip + billTotal);
		return tip;
	}

	public float getTipForEachPersonAfterChange(float tipBefore, float tipAfter) {
		return (totalTip - tipAfter) / (numberOfGuests - 1);
	}

	public List<String> getTipSplit() {
		List<String> tipList = new ArrayList<String>();

		for (int progress : guestProgress) {
			float tip = getTipFromPercent(progress);
			String roundedTip = dec.format(tip);
			tipList.add(roundedTip);
		}
		return tipList;
	}

	public List<String> getBillSplit() {
		List<String> tipList = new ArrayList<String>();

		for (int progress : guestProgress) {
			float tip = getBillFromPercent(progress);
			String roundedTip = dec.format(tip);
			tipList.add(roundedTip);
		}
		return tipList;
	}

	public List<String> getBillAndTipSplit() {
		List<String> tipList = new ArrayList<String>();

		for (int progress : guestProgress) {
			float tip = getBillAndTipFromPercent(progress);
			String roundedTip = dec.format(tip);
			tipList.add(roundedTip);
		}
		return tipList;
	}

	public void updateAllSplits() {
		if (tipSplit.isChecked() && billSplit.isChecked()) {
			List<String> billAndTips = getBillAndTipSplit();
			int i = 0;
			for (String s : billAndTips) {
				textViewList.get(i).setText("$\t" + s);
				i++;
			}
		} else if (tipSplit.isChecked() && !billSplit.isChecked()) {
			List<String> tips = getTipSplit();
			int i = 0;
			for (String s : tips) {
				textViewList.get(i).setText("$\t" + s);
				i++;
			}
		} else if (!tipSplit.isChecked() && billSplit.isChecked()) {
			List<String> bills = getBillSplit();
			int i = 0;
			for (String s : bills) {
				textViewList.get(i).setText("$\t" + s);
				i++;
			}
		} else if (!tipSplit.isChecked() && !billSplit.isChecked()) {

		}
	}

}
